# -*- coding: utf-8 -*-
from twisted.enterprise import adbapi
import MySQLdb
import MySQLdb.cursors


class MysqlPipelines(object):
    def __init__(self, dbpool):
        self.dbpool = dbpool

    @classmethod
    def from_settings(cls, settings):
        dbargs = dict(
            host=settings['MYSQL_HOST'],
            db=settings['MYSQL_DBNAME'],
            user=settings['MYSQL_USER'],
            passwd=settings['MYSQL_PASSWD'],
            charset='utf8',
            cursorclass=MySQLdb.cursors.DictCursor,
            use_unicode=True,
        )
        dbpool = adbapi.ConnectionPool('MySQLdb', **dbargs)
        return cls(dbpool)

    # pipeline默认调用
    def process_item(self, item, spider):
        del item['is_send']
        d = self.dbpool.runInteraction(self._do_upinsert, item, spider)
        d.addErrback(self._handle_error, item, spider)
        d.addBoth(lambda _: item)
        return d

    # 将每行更新或写入数据库中
    def _do_upinsert(self, conn, item, spider):
        data_value = [] #值列表
        data_key = [] #值列表
        replace = []

        for key in item:
            replace.append('%s')
            data_value.append(item[key])
            data_key.append(key)

        replace_key_str =','.join(replace)
        data_key_str = ','.join(data_key)
        sql = "insert into weibo ("+data_key_str+") values ("+replace_key_str+")"
        conn.execute(sql, tuple(data_value))
        pass

    # 异常处理
    def _handle_error(self, failue, item, spider):
        print failue
        # log.err(failure)


